<template>
  <div 
    class="side-menu" 
    :class="{ collapsed: isCollapsed }"
    @transitionend="handleTransitionEnd"
  >
    <div class="menu-header">
      <h2 v-show="!isCollapsed" @click="router.push('/')">Mapbox-gl Demos</h2>
      <button class="collapse-btn" @click="toggleCollapse">
        {{ isCollapsed ? '→' : '←' }}
      </button>
    </div>
    <nav class="menu-items">
      <router-link
        v-for="route in demoRoutes"
        :key="route.path"
        :to="route.path"
        class="menu-item"
        :title="isCollapsed ? route.name : ''"
      >
        <span class="menu-icon">📍</span>
        <span v-show="!isCollapsed" class="menu-text">{{ route.name }}</span>
      </router-link>
    </nav>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { getMapInstance } from '@/utils/mapUtils'

const router = useRouter()
const isCollapsed = ref(false)

const demoRoutes = computed(() => {
  return router.getRoutes()
    .filter(route => route.path !== '/' && !route.path.includes('/:'))
    .map(route => ({
      path: route.path,
      name: route.name as string
    }))
})

const handleTransitionEnd = (e: TransitionEvent) => {
  if (e.propertyName === 'width') {
    console.log('handleTransitionEnd', e)
    // window.dispatchEvent(new Event('resize'))
    getMapInstance()?.resize()
  }
}

const toggleCollapse = () => {
  isCollapsed.value = !isCollapsed.value
}
</script>

<style scoped>
.side-menu {
  width: 250px;
  height: 100vh;
  background: #2c3e50;
  color: white;
  transition: all 0.3s ease;
  display: flex;
  flex-direction: column;
}

.side-menu.collapsed {
  width: 60px;
}

.menu-header {
  padding: 1rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.menu-header h2 {
  margin: 0;
  font-size: 1.2rem;
  white-space: nowrap;
}

.collapse-btn {
  background: none;
  border: none;
  color: white;
  cursor: pointer;
  padding: 5px 10px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.collapse-btn:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.menu-items {
  display: flex;
  flex-direction: column;
  padding: 1rem 0;
  overflow-y: auto;
  overflow-x: hidden;
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 0.8rem 1rem;
  text-decoration: none;
  color: white;
  transition: background-color 0.3s;
  white-space: nowrap;
}

.menu-item:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.menu-icon {
  margin-right: 1rem;
}

.router-link-active {
  background-color: rgba(255, 255, 255, 0.2);
}
</style> 